package com.bitdf.txing.chat.exeception;

import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.common.utils.MyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author Lizhiwei
 * @date 2023/1/27 1:02:04
 * 注释：
 */
@Slf4j
@RestControllerAdvice(basePackages = "com.bitdf.txing.chat.controller")
public class ExceptionControllerAdvice {
    /**
     * 数据库插入数据时值重复异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = {SQLIntegrityConstraintViolationException.class, DuplicateKeyException.class})
    public R handleElasticsearchStatusException(Exception e){
        e.printStackTrace();
        return R.error(TxCodeEnume.COMMON_DATABASE_RECORD_EXIST_EXCEPTION);
    }


    @ExceptionHandler(value = {MyException.class})
    public R handleRRException(MyException e){
        e.printStackTrace();
        if (e.getCode()==9001){
            return R.error(TxCodeEnume.UPDATE_ES_FAILURE_EXCEPTION.getCode(), TxCodeEnume.UPDATE_ES_FAILURE_EXCEPTION.getMsg());
        }
        return R.error(e.getCode(),e.getMsg());
    }

    /**
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e){
        e.printStackTrace();
        return R.error(TxCodeEnume.COMMON_SYSTEM_UNKNOWN_EXCEPTION);
    }

}
